﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace demoReflection
{
    public class TypeManager
    {
        private static string xmlFileName = "/../../TypeCollection.xml";
        private static Dictionary<string, Type> typeCollection;
        
        public static Dictionary<string, Type> GetTypeCollection()
        {
            if (typeCollection == null || typeCollection.Count == 0)
            {
                GetTypeCollectionFromXML(xmlFileName);
            }
            return typeCollection;
        }

        private static void GetTypeCollectionFromXML(string fileName)
        {
            typeCollection = new Dictionary<string, Type>();
            XmlDocument doc = new XmlDocument();
            doc.Load(fileName);
            
            XmlNode nodeCollection = doc.SelectSingleNode("typecollection");
            XmlNodeList nodeItems = nodeCollection.SelectNodes("item");
            for (int i = 0; i < nodeItems.Count; i++)
            {
                string key = nodeItems[i].SelectSingleNode("key").InnerText;
                string value = nodeItems[i].SelectSingleNode("value").InnerText;
                typeCollection.Add(key, Type.GetType(value));
            }
        }
    }
}
